package com.doyens.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.doyens.reggie.commons.BaseContext;
import com.doyens.reggie.domain.AddressBook;
import com.doyens.reggie.domain.OrderDetail;
import com.doyens.reggie.domain.Orders;
import com.doyens.reggie.domain.ShoppingCart;
import com.doyens.reggie.mapper.OrderDetailMapper;
import com.doyens.reggie.mapper.OrdersMapper;
import com.doyens.reggie.service.AddressBookService;
import com.doyens.reggie.service.OrderDetailService;
import com.doyens.reggie.service.OrdersService;
import com.doyens.reggie.service.ShoppingCartService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class OrdersServiceImpl extends ServiceImpl<OrdersMapper, Orders> implements OrdersService {

    @Autowired
    private AddressBookService addressBookService;

    @Autowired
    private ShoppingCartService shoppingCartService;

    @Autowired
    private OrderDetailService orderDetailService;


    @Override
    public void submit(Orders orders) {
        // 查询当前登录用户的购物车数据 计算总金额
        List<ShoppingCart> shoppingCartList = shoppingCartService.list(new LambdaQueryWrapper<ShoppingCart>().eq(ShoppingCart::getUserId, BaseContext.getCurrentId()));

        BigDecimal sum = new BigDecimal(0); //120

        for (ShoppingCart shoppingCart : shoppingCartList) {

            Integer number = shoppingCart.getNumber();
            BigDecimal bigNumber = new BigDecimal(number);
            BigDecimal amount = shoppingCart.getAmount();
            BigDecimal mul = amount.multiply(bigNumber);
            sum = sum.add(mul);
        }

        //1.齐全相关信息
        orders.setNumber(IdWorker.getIdStr());
        orders.setStatus(2);
        orders.setUserId(BaseContext.getCurrentId());
        orders.setOrderTime(LocalDateTime.now());
        orders.setCheckoutTime(LocalDateTime.now());
        orders.setAmount(sum);
        // 查询收货地址
        AddressBook addressBook = addressBookService.getById(orders.getAddressBookId());
        orders.setPhone(addressBook.getPhone());
        orders.setAddress(addressBook.getDetail());
        orders.setConsignee(addressBook.getConsignee());

        // 下单
        this.save(orders);

        // 获取订单的id
        Long orderId = orders.getId();

        List<OrderDetail> orderDetailList = shoppingCartList.stream().map(shoppingCart -> {
            OrderDetail orderDetail = new OrderDetail();

            orderDetail.setName(shoppingCart.getName());
            orderDetail.setImage(shoppingCart.getImage());
            orderDetail.setDishId(shoppingCart.getDishId());
            orderDetail.setSetmealId(shoppingCart.getSetmealId());
            orderDetail.setDishFlavor(shoppingCart.getDishFlavor());
            orderDetail.setNumber(shoppingCart.getNumber());
            orderDetail.setAmount(shoppingCart.getAmount());

            orderDetail.setOrderId(orderId);
            return orderDetail;
        }).collect(Collectors.toList());

        //添加到明细表
        orderDetailService.saveBatch(orderDetailList);
        // 删除当前登录用户的购物车数据
        shoppingCartService.remove(new LambdaQueryWrapper<ShoppingCart>().eq(ShoppingCart::getUserId, BaseContext.getCurrentId()));
    }

}


